!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine tvd_lf (ro, mx, my, mz, bx, by, bz, en, az, po, gx, gy, x, y,                                                           &
   nx, ny, ng, n_neighs, dx, dy, dt, max_et, gm, cd, cfl, div_clean_flag, resistivity_flag, riemann_solver_flag,                   &
   fxro_bnd, fxmx_bnd, fxmy_bnd, fxmz_bnd, fxbx_bnd, fxby_bnd, fxbz_bnd, fxen_bnd, fxpo_bnd,                                       &
   fyro_bnd, fymx_bnd, fymy_bnd, fymz_bnd, fybx_bnd, fyby_bnd, fybz_bnd, fyen_bnd, fypo_bnd)
!==================================================================================================================================|

   use interface_check_state
   implicit none

   integer(4), intent(in) :: nx, ny, ng, n_neighs, div_clean_flag, resistivity_flag, riemann_solver_flag
   real(8), intent(in) :: dx, dy, dt, gm, cd, cfl, x(nx), y(ny)
   real(8), intent(inout) :: max_et
   real(8), dimension(nx, ny), intent(inout) :: ro, mx, my, mz, bx, by, bz, en, az, po, gx, gy
   real(8), dimension(ny, n_neighs), intent(inout) ::                                                                              &
      fxro_bnd, fxmx_bnd, fxmy_bnd, fxmz_bnd, fxbx_bnd, fxby_bnd, fxbz_bnd, fxen_bnd, fxpo_bnd
   real(8), dimension(nx, n_neighs), intent(inout) ::                                                                              &
      fyro_bnd, fymx_bnd, fymy_bnd, fymz_bnd, fybx_bnd, fyby_bnd, fybz_bnd, fyen_bnd, fypo_bnd

   integer(4) :: i, ip1, im1, j, jp1, jm1
   integer(4) :: sgn

   real(8) :: limiter, ch, ch2, cp2
   real(8) :: dt_dx, dt_dy, u1, u2, vx, vy, vz, b2, v2, c2, s2, ca2, cfx, cfy, pr, max_speed, gmm1, dx2, dy2, eps

   real(8), dimension(nx, ny) :: ro_pre, mx_pre, my_pre, mz_pre, bx_pre, by_pre, bz_pre, en_pre, az_pre, po_pre
   real(8), dimension(nx, ny) :: ro_lx, mx_lx, my_lx, mz_lx, bx_lx, by_lx, bz_lx, en_lx, po_lx
   real(8), dimension(nx, ny) :: ro_rx, mx_rx, my_rx, mz_rx, bx_rx, by_rx, bz_rx, en_rx, po_rx
   real(8), dimension(nx, ny) :: ro_ly, mx_ly, my_ly, mz_ly, bx_ly, by_ly, bz_ly, en_ly, po_ly
   real(8), dimension(nx, ny) :: ro_ry, mx_ry, my_ry, mz_ry, bx_ry, by_ry, bz_ry, en_ry, po_ry
   real(8), dimension(nx, ny) :: fxro, fxmx, fxmy, fxmz, fxbx, fxby, fxbz, fxen, fxpo
   real(8), dimension(nx, ny) :: fyro, fymx, fymy, fymz, fybx, fyby, fybz, fyen, fypo
   real(8), dimension(nx, ny) :: bx_resis, by_resis, bz_resis, en_resis, az_resis !, s !, te_cond_x, te_cond_y

   real(8) :: fxro_l, fxmx_l, fxmy_l, fxmz_l, fxbx_l, fxby_l, fxbz_l, fxen_l, fxpo_l
   real(8) :: fxro_r, fxmx_r, fxmy_r, fxmz_r, fxbx_r, fxby_r, fxbz_r, fxen_r, fxpo_r
   real(8) :: fyro_l, fymx_l, fymy_l, fymz_l, fybx_l, fyby_l, fybz_l, fyen_l, fypo_l
   real(8) :: fyro_r, fymx_r, fymy_r, fymz_r, fybx_r, fyby_r, fybz_r, fyen_r, fypo_r

!----------------------------------------------------------------------------------------------------------------------------------|
!  left and right reconstructed value
!----------------------------------------------------------------------------------------------------------------------------------|
   eps = 1.0d-12
   dt_dx = dt / dx
   dt_dy = dt / dy
   gmm1 = gm - 1.0d0
   dx2 = dx * 2.0d0
   dy2 = dy * 2.0d0
   ch = cfl * min (dx, dy) / dt * div_clean_flag
   ch2 = ch * ch
!   cp2 = ch * cd / 6.0d0 + eps
   cp2 = -dt * ch2 / log (cd) + eps

   do j = 2, ny - 1
      jp1 = j + 1
      jm1 = j - 1
      do i = 2, nx - 1
         ip1 = i + 1
         im1 = i - 1

         u1 = ro(i, j) - ro(im1, j)
         u2 = ro(ip1, j) - ro(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         ro_lx(i, j) = ro(i, j) + 0.5d0 * limiter
         ro_rx(i, j) = ro(i, j) - 0.5d0 * limiter

         u1 = mx(i, j) - mx(im1, j)
         u2 = mx(ip1, j) - mx(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         mx_lx(i, j) = mx(i, j) + 0.5d0 * limiter
         mx_rx(i, j) = mx(i, j) - 0.5d0 * limiter

         u1 = my(i, j) - my(im1, j)
         u2 = my(ip1, j) - my(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         my_lx(i, j) = my(i, j) + 0.5d0 * limiter
         my_rx(i, j) = my(i, j) - 0.5d0 * limiter

         u1 = mz(i, j) - mz(im1, j)
         u2 = mz(ip1, j) - mz(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         mz_lx(i, j) = mz(i, j) + 0.5d0 * limiter
         mz_rx(i, j) = mz(i, j) - 0.5d0 * limiter

         u1 = bx(i, j) - bx(im1, j)
         u2 = bx(ip1, j) - bx(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         bx_lx(i, j) = bx(i, j) + 0.5d0 * limiter
         bx_rx(i, j) = bx(i, j) - 0.5d0 * limiter

         u1 = by(i, j) - by(im1, j)
         u2 = by(ip1, j) - by(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         by_lx(i, j) = by(i, j) + 0.5d0 * limiter
         by_rx(i, j) = by(i, j) - 0.5d0 * limiter

         u1 = bz(i, j) - bz(im1, j)
         u2 = bz(ip1, j) - bz(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         bz_lx(i, j) = bz(i, j) + 0.5d0 * limiter
         bz_rx(i, j) = bz(i, j) - 0.5d0 * limiter

         u1 = en(i, j) - en(im1, j)
         u2 = en(ip1, j) - en(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         en_lx(i, j) = en(i, j) + 0.5d0 * limiter
         en_rx(i, j) = en(i, j) - 0.5d0 * limiter

         u1 = po(i, j) - po(im1, j)
         u2 = po(ip1, j) - po(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         po_lx(i, j) = po(i, j) + 0.5d0 * limiter
         po_rx(i, j) = po(i, j) - 0.5d0 * limiter

         u1 = ro(i, j) - ro(i, jm1)
         u2 = ro(i, jp1) - ro(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         ro_ly(i, j) = ro(i, j) + 0.5d0 * limiter
         ro_ry(i, j) = ro(i, j) - 0.5d0 * limiter

         u1 = mx(i, j) - mx(i, jm1)
         u2 = mx(i, jp1) - mx(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         mx_ly(i, j) = mx(i, j) + 0.5d0 * limiter
         mx_ry(i, j) = mx(i, j) - 0.5d0 * limiter

         u1 = my(i, j) - my(i, jm1)
         u2 = my(i, jp1) - my(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         my_ly(i, j) = my(i, j) + 0.5d0 * limiter
         my_ry(i, j) = my(i, j) - 0.5d0 * limiter

         u1 = mz(i, j) - mz(i, jm1)
         u2 = mz(i, jp1) - mz(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         mz_ly(i, j) = mz(i, j) + 0.5d0 * limiter
         mz_ry(i, j) = mz(i, j) - 0.5d0 * limiter

         u1 = bx(i, j) - bx(i, jm1)
         u2 = bx(i, jp1) - bx(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         bx_ly(i, j) = bx(i, j) + 0.5d0 * limiter
         bx_ry(i, j) = bx(i, j) - 0.5d0 * limiter

         u1 = by(i, j) - by(i, jm1)
         u2 = by(i, jp1) - by(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         by_ly(i, j) = by(i, j) + 0.5d0 * limiter
         by_ry(i, j) = by(i, j) - 0.5d0 * limiter

         u1 = bz(i, j) - bz(i, jm1)
         u2 = bz(i, jp1) - bz(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         bz_ly(i, j) = bz(i, j) + 0.5d0 * limiter
         bz_ry(i, j) = bz(i, j) - 0.5d0 * limiter

         u1 = en(i, j) - en(i, jm1)
         u2 = en(i, jp1) - en(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         en_ly(i, j) = en(i, j) + 0.5d0 * limiter
         en_ry(i, j) = en(i, j) - 0.5d0 * limiter

         u1 = po(i, j) - po(i, jm1)
         u2 = po(i, jp1) - po(i, j)
         if (u1 .ge. 0.0d0) then
            sgn = 1
         else
            sgn = -1
         endif
         limiter = sgn * max (0.0d0, min (abs (u1), sgn * u2))
         po_ly(i, j) = po(i, j) + 0.5d0 * limiter
         po_ry(i, j) = po(i, j) - 0.5d0 * limiter
      enddo
   enddo

!----------------------------------------------------------------------------------------------------------------------------------|
!  predictor-corrector step
!----------------------------------------------------------------------------------------------------------------------------------|
   do j = 1, ny
   do i = 1, nx
      vx = mx_lx(i, j) / ro_lx(i, j)
      vy = my_lx(i, j) / ro_lx(i, j)
      vz = mz_lx(i, j) / ro_lx(i, j)
      b2 = bx_lx(i, j) * bx_lx(i, j) + by_lx(i, j) * by_lx(i, j) + bz_lx(i, j) * bz_lx(i, j)
      v2 = vx * vx + vy * vy + vz * vz
      pr = (en_lx(i, j) - v2 * ro_lx(i, j) / 2.0d0 - b2 / 2.0d0) * gmm1
      fxro_l = mx_lx(i, j)
      fxmx_l = vx * mx_lx(i, j) + pr + b2 / 2.0d0 - bx_lx(i, j) * bx_lx(i, j)
      fxmy_l = vx * my_lx(i, j) - bx_lx(i, j) * by_lx(i, j)
      fxmz_l = vx * mz_lx(i, j) - bx_lx(i, j) * bz_lx(i, j)
      fxbx_l = po_lx(i, j)
      fxby_l = vx * by_lx(i, j) - bx_lx(i, j) * vy
      fxbz_l = vx * bz_lx(i, j) - bx_lx(i, j) * vz
      fxen_l = (en_lx(i, j) + pr + b2 / 2.0d0) * vx - (bx_lx(i, j) * vx + by_lx(i, j) * vy + bz_lx(i, j) * vz) * bx_lx(i, j)
      fxpo_l = ch2 * bx_lx(i, j)

      vx = mx_rx(i, j) / ro_rx(i, j)
      vy = my_rx(i, j) / ro_rx(i, j)
      vz = mz_rx(i, j) / ro_rx(i, j)
      b2 = bx_rx(i, j) * bx_rx(i, j) + by_rx(i, j) * by_rx(i, j) + bz_rx(i, j) * bz_rx(i, j)
      v2 = vx * vx + vy * vy + vz * vz
      pr = (en_rx(i, j) - v2 * ro_rx(i, j) / 2.0d0 - b2 / 2.0d0) * gmm1
      fxro_r = mx_rx(i, j)
      fxmx_r = vx * mx_rx(i, j) + pr + b2 / 2.0d0 - bx_rx(i, j) * bx_rx(i, j)
      fxmy_r = vx * my_rx(i, j) - bx_rx(i, j) * by_rx(i, j)
      fxmz_r = vx * mz_rx(i, j) - bx_rx(i, j) * bz_rx(i, j)
      fxbx_r = po_rx(i, j)
      fxby_r = vx * by_rx(i, j) - bx_rx(i, j) * vy
      fxbz_r = vx * bz_rx(i, j) - bx_rx(i, j) * vz
      fxen_r = (en_rx(i, j) + pr + b2 / 2.0d0) * vx - (bx_rx(i, j) * vx + by_rx(i, j) * vy + bz_rx(i, j) * vz) * bx_rx(i, j)
      fxpo_r = ch2 * bx_rx(i, j)

      vx = mx_ly(i, j) / ro_ly(i, j)
      vy = my_ly(i, j) / ro_ly(i, j)
      vz = mz_ly(i, j) / ro_ly(i, j)
      b2 = bx_ly(i, j) * bx_ly(i, j) + by_ly(i, j) * by_ly(i, j) + bz_ly(i, j) * bz_ly(i, j)
      v2 = vx * vx + vy * vy + vz * vz
      pr = (en_ly(i, j) - v2 * ro_ly(i, j) / 2.0d0 - b2 / 2.0d0) * gmm1
      fyro_l = my_ly(i, j)
      fymx_l = vy * mx_ly(i, j) - by_ly(i, j) * bx_ly(i, j)
      fymy_l = vy * my_ly(i, j) + pr + b2 / 2.0d0 - by_ly(i, j) * by_ly(i, j)
      fymz_l = vy * mz_ly(i, j) - by_ly(i, j) * bz_ly(i, j)
      fybx_l = vy * bx_ly(i, j) - by_ly(i, j) * vx
      fyby_l = po_ly(i, j)
      fybz_l = vy * bz_ly(i, j) - by_ly(i, j) * vz
      fyen_l = (en_ly(i, j) + pr + b2 / 2.0d0) * vy - (bx_ly(i, j) * vx + by_ly(i, j) * vy + bz_ly(i, j) * vz) * by_ly(i, j)
      fypo_l = ch2 * by_ly(i, j)

      vx = mx_ry(i, j) / ro_ry(i, j)
      vy = my_ry(i, j) / ro_ry(i, j)
      vz = mz_ry(i, j) / ro_ry(i, j)
      b2 = bx_ry(i, j) * bx_ry(i, j) + by_ry(i, j) * by_ry(i, j) + bz_ry(i, j) * bz_ry(i, j)
      v2 = vx * vx + vy * vy + vz * vz
      pr = (en_ry(i, j) - v2 * ro_ry(i, j) / 2.0d0 - b2 / 2.0d0) * gmm1
      fyro_r = my_ry(i, j)
      fymx_r = vy * mx_ry(i, j) - by_ry(i, j) * bx_ry(i, j)
      fymy_r = vy * my_ry(i, j) + pr + b2 / 2.0d0 - by_ry(i, j) * by_ry(i, j)
      fymz_r = vy * mz_ry(i, j) - by_ry(i, j) * bz_ry(i, j)
      fybx_r = vy * bx_ry(i, j) - by_ry(i, j) * vx
      fyby_r = po_ry(i, j)
      fybz_r = vy * bz_ry(i, j) - by_ry(i, j) * vz
      fyen_r = (en_ry(i, j) + pr + b2 / 2.0d0) * vy - (bx_ry(i, j) * vx + by_ry(i, j) * vy + bz_ry(i, j) * vz) * by_ry(i, j)
      fypo_r = ch2 * by_ry(i, j)

      ro_pre(i, j) = ro(i, j) - 0.5d0 * dt_dx * (fxro_l - fxro_r) - 0.5d0 * dt_dy * (fyro_l - fyro_r)
      mx_pre(i, j) = mx(i, j) - 0.5d0 * dt_dx * (fxmx_l - fxmx_r) - 0.5d0 * dt_dy * (fymx_l - fymx_r)
      my_pre(i, j) = my(i, j) - 0.5d0 * dt_dx * (fxmy_l - fxmy_r) - 0.5d0 * dt_dy * (fymy_l - fymy_r)
      mz_pre(i, j) = mz(i, j) - 0.5d0 * dt_dx * (fxmz_l - fxmz_r) - 0.5d0 * dt_dy * (fymz_l - fymz_r)
      bx_pre(i, j) = bx(i, j) - 0.5d0 * dt_dx * (fxbx_l - fxbx_r) - 0.5d0 * dt_dy * (fybx_l - fybx_r)
      by_pre(i, j) = by(i, j) - 0.5d0 * dt_dx * (fxby_l - fxby_r) - 0.5d0 * dt_dy * (fyby_l - fyby_r)
      bz_pre(i, j) = bz(i, j) - 0.5d0 * dt_dx * (fxbz_l - fxbz_r) - 0.5d0 * dt_dy * (fybz_l - fybz_r)
      en_pre(i, j) = en(i, j) - 0.5d0 * dt_dx * (fxen_l - fxen_r) - 0.5d0 * dt_dy * (fyen_l - fyen_r)
      po_pre(i, j) = po(i, j) - 0.5d0 * dt_dx * (fxpo_l - fxpo_r) - 0.5d0 * dt_dy * (fypo_l - fypo_r)
      az_pre(i, j) = az(i, j)
   enddo
   enddo

   call check_state(ro_pre, mx_pre, my_pre, mz_pre, bx_pre, by_pre, bz_pre, en_pre, x, y, nx, ny, gm)

   if (resistivity_flag .eq. 0) then
      do j = 1, ny
      do i = 1, nx
         bx_resis(i, j) = 0.0d0
         by_resis(i, j) = 0.0d0
         bz_resis(i, j) = 0.0d0
         en_resis(i, j) = 0.0d0
         az_resis(i, j) = 0.0d0
      enddo
      enddo
      max_et = 0.0d0
   else
      call resistivity(bx_resis, by_resis, bz_resis, en_resis, az_resis,                                                           &
         ro, mx, my, mz, bx, by, bz, en, x, y, dx, dy, nx, ny, max_et)
   endif

!   call thermal_conduction (te_cond_x, te_cond_y, ro, mx, my, mz, bx, by, bz, en, thermal_amp, gm, dx, dy, nx, ny)

   call source_term(mx_pre, my_pre, mz_pre, bx_pre, by_pre, bz_pre, en_pre, az_pre, po_pre,                                        &
      ro, mx, my, bx, by, bz, po, gx, gy, bx_resis, by_resis, bz_resis, en_resis, az_resis,                                        &
      ch2, cp2, dx, dy, dt / 2.0d0, nx, ny, div_clean_flag)

!----------------------------------------------------------------------------------------------------------------------------------|
!  left and righe reconstructed value
!----------------------------------------------------------------------------------------------------------------------------------|
   do j = 1, ny
   do i = 1, nx

      ro_lx(i, j) = ro_pre(i, j) + ro_lx(i, j) - ro(i, j)
      ro_rx(i, j) = ro_pre(i, j) + ro_rx(i, j) - ro(i, j)
      mx_lx(i, j) = mx_pre(i, j) + mx_lx(i, j) - mx(i, j)
      mx_rx(i, j) = mx_pre(i, j) + mx_rx(i, j) - mx(i, j)
      my_lx(i, j) = my_pre(i, j) + my_lx(i, j) - my(i, j)
      my_rx(i, j) = my_pre(i, j) + my_rx(i, j) - my(i, j)
      mz_lx(i, j) = mz_pre(i, j) + mz_lx(i, j) - mz(i, j)
      mz_rx(i, j) = mz_pre(i, j) + mz_rx(i, j) - mz(i, j)
      bx_lx(i, j) = bx_pre(i, j) + bx_lx(i, j) - bx(i, j)
      bx_rx(i, j) = bx_pre(i, j) + bx_rx(i, j) - bx(i, j)
      by_lx(i, j) = by_pre(i, j) + by_lx(i, j) - by(i, j)
      by_rx(i, j) = by_pre(i, j) + by_rx(i, j) - by(i, j)
      bz_lx(i, j) = bz_pre(i, j) + bz_lx(i, j) - bz(i, j)
      bz_rx(i, j) = bz_pre(i, j) + bz_rx(i, j) - bz(i, j)
      en_lx(i, j) = en_pre(i, j) + en_lx(i, j) - en(i, j)
      en_rx(i, j) = en_pre(i, j) + en_rx(i, j) - en(i, j)
      po_lx(i, j) = po_pre(i, j) + po_lx(i, j) - po(i, j)
      po_rx(i, j) = po_pre(i, j) + po_rx(i, j) - po(i, j)

      ro_ly(i, j) = ro_pre(i, j) + ro_ly(i, j) - ro(i, j)
      ro_ry(i, j) = ro_pre(i, j) + ro_ry(i, j) - ro(i, j)
      mx_ly(i, j) = mx_pre(i, j) + mx_ly(i, j) - mx(i, j)
      mx_ry(i, j) = mx_pre(i, j) + mx_ry(i, j) - mx(i, j)
      my_ly(i, j) = my_pre(i, j) + my_ly(i, j) - my(i, j)
      my_ry(i, j) = my_pre(i, j) + my_ry(i, j) - my(i, j)
      mz_ly(i, j) = mz_pre(i, j) + mz_ly(i, j) - mz(i, j)
      mz_ry(i, j) = mz_pre(i, j) + mz_ry(i, j) - mz(i, j)
      bx_ly(i, j) = bx_pre(i, j) + bx_ly(i, j) - bx(i, j)
      bx_ry(i, j) = bx_pre(i, j) + bx_ry(i, j) - bx(i, j)
      by_ly(i, j) = by_pre(i, j) + by_ly(i, j) - by(i, j)
      by_ry(i, j) = by_pre(i, j) + by_ry(i, j) - by(i, j)
      bz_ly(i, j) = bz_pre(i, j) + bz_ly(i, j) - bz(i, j)
      bz_ry(i, j) = bz_pre(i, j) + bz_ry(i, j) - bz(i, j)
      en_ly(i, j) = en_pre(i, j) + en_ly(i, j) - en(i, j)
      en_ry(i, j) = en_pre(i, j) + en_ry(i, j) - en(i, j)
      po_ly(i, j) = po_pre(i, j) + po_ly(i, j) - po(i, j)
      po_ry(i, j) = po_pre(i, j) + po_ry(i, j) - po(i, j)

   enddo
   enddo

!----------------------------------------------------------------------------------------------------------------------------------|
!  Riemann solver
!----------------------------------------------------------------------------------------------------------------------------------|
   if (riemann_solver_flag .eq. 1) then
      call hlld_x (fxro, fxmx, fxmy, fxmz, fxbx, fxby, fxbz, fxen,                                                                 &
         ro_lx, mx_lx, my_lx, mz_lx, bx_lx, by_lx, bz_lx, en_lx, ro_rx, mx_rx, my_rx, mz_rx, bx_rx, by_rx, bz_rx, en_rx, nx, ny, gm)
      call hlld_y (fyro, fymy, fymx, fymz, fyby, fybx, fybz, fyen,                                                                 &
         ro_ly, my_ly, mx_ly, mz_ly, by_ly, bx_ly, bz_ly, en_ly, ro_ry, my_ry, mx_ry, mz_ry, by_ry, bx_ry, bz_ry, en_ry, nx, ny, gm)
   elseif (riemann_solver_flag .eq. 2) then
      call hllc_x (fxro, fxmx, fxmy, fxmz, fxbx, fxby, fxbz, fxen,                                                                 &
         ro_lx, mx_lx, my_lx, mz_lx, bx_lx, by_lx, bz_lx, en_lx, ro_rx, mx_rx, my_rx, mz_rx, bx_rx, by_rx, bz_rx, en_rx, nx, ny, gm)
      call hllc_y (fyro, fymy, fymx, fymz, fyby, fybx, fybz, fyen,                                                                 &
         ro_ly, my_ly, mx_ly, mz_ly, by_ly, bx_ly, bz_ly, en_ly, ro_ry, my_ry, mx_ry, mz_ry, by_ry, bx_ry, bz_ry, en_ry, nx, ny, gm)
   elseif (riemann_solver_flag .eq. 3) then
      call roe_x (fxro, fxmx, fxmy, fxmz, fxbx, fxby, fxbz, fxen,                                                                  &
         ro_lx, mx_lx, my_lx, mz_lx, bx_lx, by_lx, bz_lx, en_lx, ro_rx, mx_rx, my_rx, mz_rx, bx_rx, by_rx, bz_rx, en_rx, nx, ny, gm)
      call roe_y (fyro, fymy, fymx, fymz, fyby, fybx, fybz, fyen,                                                                 &
         ro_ly, my_ly, mx_ly, mz_ly, by_ly, bx_ly, bz_ly, en_ly, ro_ry, my_ry, mx_ry, mz_ry, by_ry, bx_ry, bz_ry, en_ry, nx, ny, gm)
   endif

!      do j = 1, ny
!      do i = 1, nx
!         vx = mx_pre(i, j) / ro_pre(i, j)
!         vy = my_pre(i, j) / ro_pre(i, j)
!         vz = mz_pre(i, j) / ro_pre(i, j)
!         b2 = bx_pre(i, j) * bx_pre(i, j) + by_pre(i, j) * by_pre(i, j) + bz_pre(i, j) * bz_pre(i, j)
!         v2 = vx * vx + vy * vy + vz * vz
!         pr = (en_pre(i, j) - v2 * ro_pre(i, j) / 2.0d0 - b2 / 2.0d0) * gmm1
!         c2 = gm * pr
!         s2 = c2 + b2
!         ca2 = bx_pre(i, j) * bx_pre(i, j)
!         cfx = sqrt((s2 + sqrt(s2 * s2 - 4.0d0 * c2 * ca2)) / ro_pre(i, j) / 2.0d0)
!         ca2 = by_pre(i, j) * by_pre(i, j)
!         cfy = sqrt((s2 + sqrt(s2 * s2 - 4.0d0 * c2 * ca2)) / ro_pre(i, j) / 2.0d0)
!         s(i, j) = sqrt ((cfx + abs (vx)) ** 2 + (cfy + abs (vy)) ** 2)
!         if (isnan(s(i, j)) .or. s(i, j) .gt. 1.0d0 / eps .or. s(i, j) .lt. -1.0d0 / eps) s(i, j) = 0.0d0
!      enddo
!      enddo

   do j = 1, ny - 1
      jp1 = j + 1
      do i = 1, nx - 1
         ip1 = i + 1

         vx = mx_lx(i, j) / ro_lx(i, j)
         vy = my_lx(i, j) / ro_lx(i, j)
         vz = mz_lx(i, j) / ro_lx(i, j)
         b2 = bx_lx(i, j) * bx_lx(i, j) + by_lx(i, j) * by_lx(i, j) + bz_lx(i, j) * bz_lx(i, j)
         v2 = vx * vx + vy * vy + vz * vz
         pr = (en_lx(i, j) - v2 * ro_lx(i, j) / 2.0d0 - b2 / 2.0d0) * gmm1
         c2 = gm * pr
         s2 = c2 + b2
         ca2 = bx_lx(i, j) * bx_lx(i, j)
         cfx = sqrt((s2 + sqrt(s2 * s2 - 4.0d0 * c2 * ca2)) / ro_lx(i, j) / 2.0d0)
         max_speed = cfx + abs (vx)

         if (riemann_solver_flag .eq. 0) then
            fxro_l = mx_lx(i, j)
            fxmx_l = vx * mx_lx(i, j) + pr + b2 / 2.0d0 - bx_lx(i, j) * bx_lx(i, j)
            fxmy_l = vx * my_lx(i, j) - bx_lx(i, j) * by_lx(i, j)
            fxmz_l = vx * mz_lx(i, j) - bx_lx(i, j) * bz_lx(i, j)
            fxbx_l = po_lx(i, j)
            fxby_l = vx * by_lx(i, j) - bx_lx(i, j) * vy
            fxbz_l = vx * bz_lx(i, j) - bx_lx(i, j) * vz
            fxen_l = (en_lx(i, j) + pr + b2 / 2.0d0) * vx - (bx_lx(i, j) * vx + by_lx(i, j) * vy + bz_lx(i, j) * vz) * bx_lx(i, j)
         endif
         fxpo_l = ch2 * bx_lx(i, j)

         vx = mx_rx(ip1, j) / ro_rx(ip1, j)
         vy = my_rx(ip1, j) / ro_rx(ip1, j)
         vz = mz_rx(ip1, j) / ro_rx(ip1, j)
         b2 = bx_rx(ip1, j) * bx_rx(ip1, j) + by_rx(ip1, j) * by_rx(ip1, j) + bz_rx(ip1, j) * bz_rx(ip1, j)
         v2 = vx * vx + vy * vy + vz * vz
         pr = (en_rx(ip1, j) - v2 * ro_rx(ip1, j) / 2.0d0 - b2 / 2.0d0) * gmm1
         c2 = gm * pr
         s2 = c2 + b2
         ca2 = bx_rx(ip1, j) * bx_rx(ip1, j)
         cfx = sqrt ((s2 + sqrt (s2 * s2 - 4.0d0 * c2 * ca2)) / ro_rx(ip1, j) / 2.0d0)
         max_speed = max (cfx + abs (vx), max_speed)

         if (riemann_solver_flag .eq. 0) then
            fxro_r = mx_rx(ip1, j)
            fxmx_r = vx * mx_rx(ip1, j) + pr + b2 / 2.0d0 - bx_rx(ip1, j) * bx_rx(ip1, j)
            fxmy_r = vx * my_rx(ip1, j) - bx_rx(ip1, j) * by_rx(ip1, j)
            fxmz_r = vx * mz_rx(ip1, j) - bx_rx(ip1, j) * bz_rx(ip1, j)
            fxbx_r = po_rx(i, j)
            fxby_r = vx * by_rx(ip1, j) - bx_rx(ip1, j) * vy
            fxbz_r = vx * bz_rx(ip1, j) - bx_rx(ip1, j) * vz
            fxen_r = (en_rx(ip1, j) + pr + b2 / 2.0d0) * vx - (bx_rx(ip1, j) * vx + by_rx(ip1, j) * vy + bz_rx(ip1, j) * vz) *     &
               bx_rx(ip1, j)
         endif
         fxpo_r = ch2 * bx_rx(ip1, j)
         
         if (riemann_solver_flag .eq. 0) then
            fxro(i, j) = 0.5d0 * (fxro_l + fxro_r - max_speed * (ro_rx(ip1, j) - ro_lx(i, j)))
            fxmx(i, j) = 0.5d0 * (fxmx_l + fxmx_r - max_speed * (mx_rx(ip1, j) - mx_lx(i, j)))
            fxmy(i, j) = 0.5d0 * (fxmy_l + fxmy_r - max_speed * (my_rx(ip1, j) - my_lx(i, j)))
            fxmz(i, j) = 0.5d0 * (fxmz_l + fxmz_r - max_speed * (mz_rx(ip1, j) - mz_lx(i, j)))
            fxbx(i, j) = 0.5d0 * (fxbx_l + fxbx_r - max_speed * (bx_rx(ip1, j) - bx_lx(i, j)))
            fxby(i, j) = 0.5d0 * (fxby_l + fxby_r - max_speed * (by_rx(ip1, j) - by_lx(i, j)))
            fxbz(i, j) = 0.5d0 * (fxbz_l + fxbz_r - max_speed * (bz_rx(ip1, j) - bz_lx(i, j)))
            fxen(i, j) = 0.5d0 * (fxen_l + fxen_r - max_speed * (en_rx(ip1, j) - en_lx(i, j)))
         endif
         fxpo(i, j) = 0.5d0 * (fxpo_l + fxpo_r - max_speed * (po_rx(ip1, j) - po_lx(i, j)))

         vx = mx_ly(i, j) / ro_ly(i, j)
         vy = my_ly(i, j) / ro_ly(i, j)
         vz = mz_ly(i, j) / ro_ly(i, j)
         b2 = bx_ly(i, j) * bx_ly(i, j) + by_ly(i, j) * by_ly(i, j) + bz_ly(i, j) * bz_ly(i, j)
         v2 = vx * vx + vy * vy + vz * vz
         pr = (en_ly(i, j) - v2 * ro_ly(i, j) / 2.0d0 - b2 / 2.0d0) * gmm1
         c2 = gm * pr
         s2 = c2 + b2
         ca2 = by_ly(i, j) * by_ly(i, j)
         cfy = sqrt ((s2 + sqrt (s2 * s2 - 4.0d0 * c2 * ca2)) / ro_ly(i, j) / 2.0d0)
         max_speed = cfy + abs (vy)

         if (riemann_solver_flag .eq. 0) then
            fyro_l = my_ly(i, j)
            fymx_l = vy * mx_ly(i, j) - by_ly(i, j) * bx_ly(i, j)
            fymy_l = vy * my_ly(i, j) + pr + b2 / 2.0d0 - by_ly(i, j) * by_ly(i, j)
            fymz_l = vy * mz_ly(i, j) - by_ly(i, j) * bz_ly(i, j)
            fybx_l = vy * bx_ly(i, j) - by_ly(i, j) * vx
            fyby_l = po_ly(i, j)
            fybz_l = vy * bz_ly(i, j) - by_ly(i, j) * vz
            fyen_l = (en_ly(i, j) + pr + b2 / 2.0d0) * vy - (bx_ly(i, j) * vx + by_ly(i, j) * vy + bz_ly(i, j) * vz) * by_ly(i, j)
         endif
         fypo_l = ch2 * by_ly(i, j)

         vx = mx_ry(i, jp1) / ro_ry(i, jp1)
         vy = my_ry(i, jp1) / ro_ry(i, jp1)
         vz = mz_ry(i, jp1) / ro_ry(i, jp1)
         b2 = bx_ry(i, jp1) * bx_ry(i, jp1) + by_ry(i, jp1) * by_ry(i, jp1) + bz_ry(i, jp1) * bz_ry(i, jp1)
         v2 = vx * vx + vy * vy + vz * vz
         pr = (en_ry(i, jp1) - v2 * ro_ry(i, jp1) / 2.0d0 - b2 / 2.0d0) * gmm1
         c2 = gm * pr
         s2 = c2 + b2
         ca2 = by_ly(i, jp1) * by_ly(i, jp1)
         cfy = sqrt ((s2 + sqrt (s2 * s2 - 4.0d0 * c2 * ca2)) / ro_ly(i, jp1) / 2.0d0)
         max_speed = max (cfy + abs (vy), max_speed)

         if (riemann_solver_flag .eq. 0) then
            fyro_r = my_ry(i, jp1)
            fymx_r = vy * mx_ry(i, jp1) - by_ry(i, jp1) * bx_ry(i, jp1)
            fymy_r = vy * my_ry(i, jp1) + pr + b2 / 2.0d0 - by_ry(i, jp1) * by_ry(i, jp1)
            fymz_r = vy * mz_ry(i, jp1) - by_ry(i, jp1) * bz_ry(i, jp1)
            fybx_r = vy * bx_ry(i, jp1) - by_ry(i, jp1) * vx
            fyby_r = po_ry(i, j)
            fybz_r = vy * bz_ry(i, jp1) - by_ry(i, jp1) * vz
            fyen_r = (en_ry(i, jp1) + pr + b2 / 2.0d0) * vy - (bx_ry(i, jp1) * vx + by_ry(i, jp1) * vy + bz_ry(i, jp1) * vz) *     &
               by_ry(i, jp1)
         endif
         fypo_r = ch2 * by_ry(i, jp1)

         if (riemann_solver_flag .eq. 0) then
            fyro(i, j) = 0.5d0 * (fyro_l + fyro_r - max_speed * (ro_ry(i, jp1) - ro_ly(i, j)))
            fymx(i, j) = 0.5d0 * (fymx_l + fymx_r - max_speed * (mx_ry(i, jp1) - mx_ly(i, j)))
            fymy(i, j) = 0.5d0 * (fymy_l + fymy_r - max_speed * (my_ry(i, jp1) - my_ly(i, j)))
            fymz(i, j) = 0.5d0 * (fymz_l + fymz_r - max_speed * (mz_ry(i, jp1) - mz_ly(i, j)))
            fybx(i, j) = 0.5d0 * (fybx_l + fybx_r - max_speed * (bx_ry(i, jp1) - bx_ly(i, j)))
            fyby(i, j) = 0.5d0 * (fyby_l + fyby_r - max_speed * (by_ry(i, jp1) - by_ly(i, j)))
            fybz(i, j) = 0.5d0 * (fybz_l + fybz_r - max_speed * (bz_ry(i, jp1) - bz_ly(i, j)))
            fyen(i, j) = 0.5d0 * (fyen_l + fyen_r - max_speed * (en_ry(i, jp1) - en_ly(i, j)))
         endif
         fypo(i, j) = 0.5d0 * (fypo_l + fypo_r - max_speed * (po_ry(i, jp1) - po_ly(i, j)))
      enddo
   enddo

!----------------------------------------------------------------------------------------------------------------------------------|
!  final result
!----------------------------------------------------------------------------------------------------------------------------------|
   do j = 2, ny
      jm1 = j - 1
      do i = 2, nx
         im1 = i - 1
         ro(i, j) = ro(i, j) - dt_dx * (fxro(i, j) - fxro(im1, j)) - dt_dy * (fyro(i, j) - fyro(i, jm1))
         mx(i, j) = mx(i, j) - dt_dx * (fxmx(i, j) - fxmx(im1, j)) - dt_dy * (fymx(i, j) - fymx(i, jm1))
         my(i, j) = my(i, j) - dt_dx * (fxmy(i, j) - fxmy(im1, j)) - dt_dy * (fymy(i, j) - fymy(i, jm1))
         mz(i, j) = mz(i, j) - dt_dx * (fxmz(i, j) - fxmz(im1, j)) - dt_dy * (fymz(i, j) - fymz(i, jm1))
         bx(i, j) = bx(i, j) - dt_dx * (fxbx(i, j) - fxbx(im1, j)) - dt_dy * (fybx(i, j) - fybx(i, jm1))
         by(i, j) = by(i, j) - dt_dx * (fxby(i, j) - fxby(im1, j)) - dt_dy * (fyby(i, j) - fyby(i, jm1))
         bz(i, j) = bz(i, j) - dt_dx * (fxbz(i, j) - fxbz(im1, j)) - dt_dy * (fybz(i, j) - fybz(i, jm1))
         en(i, j) = en(i, j) - dt_dx * (fxen(i, j) - fxen(im1, j)) - dt_dy * (fyen(i, j) - fyen(i, jm1))
         po(i, j) = po(i, j) - dt_dx * (fxpo(i, j) - fxpo(im1, j)) - dt_dy * (fypo(i, j) - fypo(i, jm1))
      enddo
   enddo

   if (resistivity_flag .eq. 0) then
      do j = 1, ny
      do i = 1, nx
         bx_resis(i, j) = 0.0d0
         by_resis(i, j) = 0.0d0
         bz_resis(i, j) = 0.0d0
         en_resis(i, j) = 0.0d0
         az_resis(i, j) = 0.0d0
      enddo
      enddo
      max_et = 0.0d0
   else
      call resistivity (bx_resis, by_resis, bz_resis, en_resis, az_resis,                                                          &
         ro_pre, mx_pre, my_pre, mz_pre, bx_pre, by_pre, bz_pre, en_pre, x, y, dx, dy, nx, ny, max_et)
   endif

!   call thermal_conduction (te_cond_x, te_cond_y, ro_pre, mx_pre, my_pre, mz_pre, bx_pre, by_pre, bz_pre, en_pre,                  &
!      thermal_amp, gm, dx, dy, nx, ny)

   call source_term (mx, my, mz, bx, by, bz, en, az, po,                                                                           &
      ro_pre, mx_pre, my_pre, bx_pre, by_pre, bz_pre, po_pre, gx, gy,                                                              &
      bx_resis, by_resis, bz_resis, en_resis, az_resis,                                                                            &
      ch2, cp2, dx, dy, dt, nx, ny, div_clean_flag)

!----------------------------------------------------------------------------------------------------------------------------------|
!  bnd value
!----------------------------------------------------------------------------------------------------------------------------------|
   do j = 1, ny
      fxro_bnd(j, 1) = fxro(ng, j)
      fxro_bnd(j, 2) = fxro(nx - ng, j)

      fxmx_bnd(j, 1) = fxmx(ng, j)
      fxmx_bnd(j, 2) = fxmx(nx - ng, j)

      fxmy_bnd(j, 1) = fxmy(ng, j)
      fxmy_bnd(j, 2) = fxmy(nx - ng, j)

      fxmz_bnd(j, 1) = fxmz(ng, j)
      fxmz_bnd(j, 2) = fxmz(nx - ng, j)

      fxbx_bnd(j, 1) = fxbx(ng, j)
      fxbx_bnd(j, 2) = fxbx(nx - ng, j)

      fxby_bnd(j, 1) = fxby(ng, j)
      fxby_bnd(j, 2) = fxby(nx - ng, j)

      fxbz_bnd(j, 1) = fxbz(ng, j)
      fxbz_bnd(j, 2) = fxbz(nx - ng, j)

      fxen_bnd(j, 1) = fxen(ng, j)
      fxen_bnd(j, 2) = fxen(nx - ng, j)

      fxpo_bnd(j, 1) = fxpo(ng, j)
      fxpo_bnd(j, 2) = fxpo(nx - ng, j)
   enddo

   do i = 1, nx
      fyro_bnd(i, 1) = fyro(i, ng)
      fyro_bnd(i, 2) = fyro(i, ny - ng)

      fymx_bnd(i, 1) = fymx(i, ng)
      fymx_bnd(i, 2) = fymx(i, ny - ng)

      fymy_bnd(i, 1) = fymy(i, ng)
      fymy_bnd(i, 2) = fymy(i, ny - ng)

      fymz_bnd(i, 1) = fymz(i, ng)
      fymz_bnd(i, 2) = fymz(i, ny - ng)

      fybx_bnd(i, 1) = fybx(i, ng)
      fybx_bnd(i, 2) = fybx(i, ny - ng)

      fyby_bnd(i, 1) = fyby(i, ng)
      fyby_bnd(i, 2) = fyby(i, ny - ng)

      fybz_bnd(i, 1) = fybz(i, ng)
      fybz_bnd(i, 2) = fybz(i, ny - ng)

      fyen_bnd(i, 1) = fyen(i, ng)
      fyen_bnd(i, 2) = fyen(i, ny - ng)

      fypo_bnd(i, 1) = fypo(i, ng)
      fypo_bnd(i, 2) = fypo(i, ny - ng)
   enddo

   return
end subroutine tvd_lf
